define(function(require, exports){
	var $       = require("jquery");
	var util    = require('util');
	var pubjs   = require('pubjs');
	var view    = require('@base/view');
	var utilDSP = require('modules/common');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-advertiser',
				'isSubGrid': false,
				'vModel':{
					'view': {
						'addButton': true
					},
					'vmClickAdd': this.eventClickAdd
				},
				'addHrefBrand': 'serving/advertiserEdit',
				'urlFile': 'qualification/remove',
				'urlRecover': 'qualification/recover'
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			self.append($([
				'<div class="M-formHead">',
					// '<span ms-visible="view.addButton" class="ml10" pub-name="addButton" pub-mod="@base/common/drop.add" ></span>',
					'<button v-show="view.addButton" class="btn btn-primary" v-on:click="vmClickAdd">'+LANG('新建广告主')+'</button>',
					'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="advertiser/grid/advertiser.base"/>'
			].join('')));

			self.vm.mount();

			var config = {
				// 'addButton': {
				// 	'def': LANG('新建广告主'),
				// 	'href': '#admin/aptitude/edit',
				// 	'href_target': '_blank'
				// },
				'highGrid': util.extend(c.grid_config, {
					'eventDataLoad': true,
					'gridName': 'advertiser',
					'subs': ['period', 'frequency', 'region'],
                    'hasSearch': true,
					'hasFilter': true,
				})
			};
			//如果是子表格，则将id作为grid的param
			if(c.isSubGrid && c.param.condition){
				config.highGrid.param = c.param;
			}

			self.buildFromTemplate(config);

			if(!c.isSubGrid){
				self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
					'target': c.targetSidebar,
					'height': 200,
					'items': [
						{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
							'title': LANG('状态：'),
							'data': [
								{id: 0, name: LANG('有效')},
								{id: 1, name: LANG('已归档')}
							],
							'value': 0,
							'compareValue': true
						}}
					]
				});
			}
		},
		eventClickAdd: function(ev){
			var c = this.getConfig();
			pubjs.controller.navigate(c.addHrefBrand);
		},
		// 操作列点击事件
		onOperateMenuShow: function(ev) {
			var status = ev.param.IsDeleted;
			var returnList = [];

			if(!status){
				if (pubjs.checkRight('')){
					returnList.push({'id': 'edit', 'name': LANG('编辑')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'remove','name': LANG('归档')});
				}

			}else{
				if (pubjs.checkRight('')){
					// 已归档的只有'还原'操作
					returnList.push( {id: 'recover', name: LANG('还原')} );
				}
			}

			ev.returnValue = returnList;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function(ev) {
			var self = this;
			var key = ev.param[0]['key'];
			var selectedUser = ev.param[1];

			var id = +selectedUser['_id'];
			var UserName = selectedUser['UserName'];

			switch (key){
				case 'edit':
					if(id){
						window.open("#admin/aptitude/edit/"+id);
					}
					break;
				case 'remove':
					self.removeAdvertiser(id, UserName);
					break;
				case 'recover':
					pubjs.mc.send(self.getConfig('urlRecover'),{'_id': id},
						self.onRecover.bind(self)
					);
			}

		},
		//响应右边侧边栏选项
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var is = item.id;
				switch (type) {
					case 'status':
						param.IsDeleted = is;
						break;
				}
			});

			self.$.highGrid.reload(param);
		},
		//删除操作
		removeAdvertiser: function(id, name){
			if( !id ) {
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('真的要归档（%1）的客户账户吗?', name),function(isOk){
				if(isOk){
					pubjs.mc.send(self.getConfig('urlFile'),{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		onRemove: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('归档成功');
			this.reload();
		},
		onRecover: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			if(!data.IsDeleted){
				pubjs.alert('还原成功');
			}else{
				pubjs.alert('还原失败');
			}
			this.reload();
		},
		reload: function( param ) {
			this.$.highGrid.setParam( param ).reload();
		},
		onGridDataLoad: function() {

		}
		,onBatchShow: function(ev){
			var list = [];
			var batch = [
				{id: 'export', name: LANG('导出'), }
			];
			util.each(batch, function(item){
				list.push(item);
			});
			ev.returnValue = list;
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var data = ev.param[0];
			var ids = ev.param[1];
			var contents = ev.param[2];
			var key = data.key || '';

			if(!ids || !ids.length) {
				pubjs.alert(LANG('请先选择广告主'));
				return false;
			}
			var self = this;
			switch(key){
				case 'export':
					var url = utilDSP.getExportApi(this.get('highGrid'), ids);
					window.location.href = url;
					break;
			}
			return false;
		}

	});
	exports.main = Main;
});